# app.py
from api import api
from sanic import Sanic
from sanic.response import json, text
from sanic.exceptions import SanicException

app = Sanic(__name__)
app.blueprint(api)

# 中间件
@app.middleware("request")
async def token(request):
    print(request.url)
    print(request.args)
    # 从请求头中解析到的 Token <token> 或者 Bearer <token> 将会被赋值给 request.token
    # print(request.token)
    # 设置cookie
    response = text("set cookie success")
    response.cookies["test"] = "test"

@app.route("/")
async def index(request):
    return json({"hi": "hi sanic!"})

@app.route("/error")
async def error(request):
        raise SanicException("Something went wrong.", status_code=501)

app.run(host='127.0.0.1', port=8000, debug=True)